﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
using NHibernate.Linq;


namespace Perpus
{
    public partial class TampilanBuku : Form
    {
        NhibernateHelper help = new NhibernateHelper();

        public TampilanBuku()
        {
            InitializeComponent();
        }
        Perpus_Fungsi fungsi = new Perpus_Fungsi();

        private void TampilanBuku_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'dotnetDataSet.Pengarang' table. You can move, or remove it, as needed.
            this.pengarangTableAdapter.Fill(this.dotnetDataSet.Pengarang);
            // TODO: This line of code loads data into the 'dotnetDataSet.Kategori' table. You can move, or remove it, as needed.
            this.kategoriTableAdapter.Fill(this.dotnetDataSet.Kategori);
            // TODO: This line of code loads data into the 'dotnetDataSet.Penerbit' table. You can move, or remove it, as needed.
            this.penerbitTableAdapter.Fill(this.dotnetDataSet.Penerbit);
            // TODO: This line of code loads data into the 'dotnetDataSet.Buku' table. You can move, or remove it, as needed.
            this.bukuTableAdapter.Fill(this.dotnetDataSet.Buku);

        }


        private void insertBtn_Click(object sender, EventArgs e)
        {

            var factory = help.CreateSessionFactory();
            string nama_buku = textBox3.Text;
            string status = textBox4.Text;
            int denda = Convert.ToInt32(textBox5.Text);
            int idpenerbit = Convert.ToInt32(comboBox1_idPenerbit.SelectedValue.ToString());
            int idpengarang = Convert.ToInt32(comboBox_idPengarang.SelectedValue.ToString());
            int idkategori = Convert.ToInt32(comboBox_idKategori.SelectedValue.ToString());
           

            if (textBox3.Text != "" && textBox4.Text != "" && textBox5.Text != "")
            {
                try
                {
                    fungsi.TambahBuku(nama_buku, status, denda, idpenerbit, idkategori, idpengarang);
                    MessageBox.Show("Berhasil");
                }
                catch
                {
                    MessageBox.Show("GAGAL!");
                }

            }
            else
            {
                MessageBox.Show("Tidak boleh Kosong");
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            var factory = help.CreateSessionFactory();

            int kodebuku = Convert.ToInt32(textBox1.Text);
            int idpenerbit = Convert.ToInt32(comboBox1_idPenerbit.SelectedValue.ToString());
            int idpengarang = Convert.ToInt32(comboBox_idPengarang.SelectedValue.ToString());
            int idkategori = Convert.ToInt32(comboBox_idKategori.SelectedValue.ToString());

            try
            {
                using (var session = factory.OpenSession())
                {
                    using (var transaction = session.BeginTransaction())
                    {
                        var book = new Buku
                        {
                            kode_buku = kodebuku,
                            nama_buku = textBox3.Text,
                            status = textBox4.Text,
                            denda = int.Parse(textBox5.Text),
                            id_penerbit = session.Get<Penerbit>(idpenerbit),
                            id_kategori = session.Get<Kategori>(idkategori),
                            id_pengarang = session.Get<Pengarang>(idpengarang)

                        };
                            
                        session.Delete(book);

                        transaction.Commit();
                        MessageBox.Show("Data Berhasil Dihapus!");
                        dataGridView1.DataSource = dotnetDataSet.Buku;
                    }
                }

            }
            catch
            {
                MessageBox.Show("Gagal!");
            }
    }


        private void button4_Click(object sender, EventArgs e)
        {
            //DataTable data = new DataTable();
            //data.Columns.Add("buku");

            var factory = help.CreateSessionFactory();
           
                using (var session = factory.OpenSession())
                {
                    var buku = session.Query<Buku>()
                           .OrderBy(c => c.kode_buku)
                           .ToList();
                    dataGridView1.DataSource = buku;
                }
            


        }

        private void button8_Click(object sender, EventArgs e)
        {
            var factory = help.CreateSessionFactory();
            try
            {
                using (var session = factory.OpenSession())
                {
                    admin_MenuAwal menu = new admin_MenuAwal();
                    menu.Show();
                    this.Hide();
                }
            }
            catch
            {
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            admin_EditKategori e_kategori = new admin_EditKategori();
            e_kategori.Show();
            this.Hide();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            admin_EditPenerbit e_penerbit = new admin_EditPenerbit();
            e_penerbit.Show();
            this.Hide();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            admin_EditPengarang e_pengarang = new admin_EditPengarang();
            e_pengarang.Show();
            this.Hide();
        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            //int pilih = dataGridView1.SelectedRows.t; 
           // MessageBox.Show("Test");
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int i = e.RowIndex;
            textBox1.Text = dataGridView1[0, i].Value.ToString();
            textBox3.Text = dataGridView1[1, i].Value.ToString();
            textBox4.Text = dataGridView1[2, i].Value.ToString();
            textBox5.Text = dataGridView1[3, i].Value.ToString();
            comboBox1_idPenerbit.Text = dataGridView1[4, i].Value.ToString();
            comboBox_idKategori.Text = dataGridView1[5, i].Value.ToString();
            comboBox_idPengarang.Text = dataGridView1[6, i].Value.ToString();

            insertBtn.Enabled = false;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            var factory = help.CreateSessionFactory();

            int id = Convert.ToInt32(textBox1.Text);
            int idpenerbit = Convert.ToInt32(comboBox1_idPenerbit.SelectedValue.ToString());
            int idpengarang = Convert.ToInt32(comboBox_idPengarang.SelectedValue.ToString());
            int idkategori = Convert.ToInt32(comboBox_idKategori.SelectedValue.ToString());

            using (var session = factory.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    var idBuku = session.Get<Buku>(id);
                //    idBuku.id_penerbit = Convert.ToInt32(comboBox1_idPenerbit.SelectedValue);
                    idBuku.nama_buku = textBox3.Text;
                    idBuku.status = textBox4.Text;
                    idBuku.denda = Convert.ToInt32(textBox5.Text);
                    idBuku.id_penerbit = session.Get<Penerbit>(idpenerbit);
                    idBuku.id_kategori = session.Get<Kategori>(idkategori);
                    idBuku.id_pengarang = session.Get<Pengarang>(idpengarang);
                    transaction.Commit();

                }
                MessageBox.Show("Berhasil!");
                dataGridView1.DataSource = dotnetDataSet.Buku;
            }   
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void clearBtn_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            textBox3.Text = "";
            textBox5.Text = "";

            insertBtn.Enabled = true;
        }


        
    }
}
